package com.gsafety.wellsfort.service.common.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.imm.model.v20170906.GetOfficePreviewURLRequest;
import com.aliyuncs.imm.model.v20170906.GetOfficePreviewURLResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.gsafety.wellsfort.common.constant.OSSConstant;
import com.gsafety.wellsfort.common.util.ApiResponse;
import com.gsafety.wellsfort.common.util.HttpClientUtil;
import com.gsafety.wellsfort.domain.dto.ids.PreviewFileDTO;
import com.gsafety.wellsfort.domain.vo.OssFileResponse;
import com.gsafety.wellsfort.service.common.FileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class FileServiceImpl implements FileService {

    @Override
    public ApiResponse getPreviewFile(PreviewFileDTO previewFileDTO) {
        try {
            // 获取图片的扩展名
            String extensionName = previewFileDTO.getFileName().substring(previewFileDTO.getFileName().lastIndexOf(".") + 1);
            String path = null;
            if (extensionName.endsWith("png") || extensionName.endsWith("jpg") || extensionName.endsWith("jpeg")) {
                path = "/image/";
            } else {
                path = "/file/";
            }
            DefaultProfile profile = DefaultProfile.getProfile(
                    OSSConstant.REGION_ID,
                    OSSConstant.ACCESS_KEY_ID,
                    OSSConstant.ACCESS_KEY_SECRET);
            IAcsClient client = new DefaultAcsClient(profile);
            GetOfficePreviewURLRequest request = new GetOfficePreviewURLRequest();
            request.setProject(OSSConstant.PROJECT_NAME);
            request.setSrcUri("oss://" + OSSConstant.BUCKET_NAME + path + previewFileDTO.getFileName());
            GetOfficePreviewURLResponse response = client.getAcsResponse(request);
            log.info("response={}", JSON.toJSONString(response));
            return ApiResponse.success(response);
        } catch (ClientException e) {
            log.error("exception={}", e);
        } finally {
        }
        return ApiResponse.error();
    }

    /**
     * 先用不上
     * @param previewFileDTO
     * @return
     */
    @Override
    public ApiResponse refreshPreviewFile(PreviewFileDTO previewFileDTO) {
        Map<String, String> param = new HashMap<>();
        param.put("Action", "RefreshOfficePreviewToken");
        param.put("Project", OSSConstant.PROJECT_NAME);
        param.put("AccessToken", previewFileDTO.getAccessToken());
        param.put("RefreshToken", previewFileDTO.getRefreshToken());
        String response = HttpClientUtil.doPost(OSSConstant.URL, param);
        if (StringUtils.isNotEmpty(response)) {
            return ApiResponse.success(JSONObject.parseObject(response, OssFileResponse.class));
        }
        return ApiResponse.error();
    }
}
